package com.game.views
{
	import com.Connect;
	import com.connect.agreement.mclgc.Packet;
	import com.core.utils.StringUtil;
	import com.game.Group;
	import com.game.performers.Actor;
	import com.module.regrole.RegInfo;
	import com.module.regrole.RegRoleManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.List;
	
	import org.phprpc.util.PHPSerializer;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class RegRoleMediator extends Mediator
	{
		public static const NAME:String = "RegRoleMediator";
		
		public static const CREATEROLE:String = "create role";
		
		public static const SHOWROLES:String = "show roles";
		
		public function RegRoleMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		
		private function createHandler(event:MouseEvent):void
		{
			var name:String = this.rolereg.nameinput.text;
			var sex:int = int(this.rolereg.sex.selectedValue);
			var role:int = int(this.rolereg.rolegroup.selectedValue);
			var reginfo:RegInfo = new RegInfo();
			reginfo.name = StringUtil.trim(name);
			reginfo.sex = sex;
			reginfo.role = role;
			var p:Packet = new Packet(true,Group.RegRole,RegRoleManager.CreateRole,PHPSerializer.serialize(reginfo));
			Connect.server.sendPackage(p);
		}
		
		private var currole:Actor;
		
		private function selectRoleHandler(event:Event):void
		{
			this.rolereg.enterGameBtn.enabled = true;
			currole = (event.currentTarget as List).selectedItem as Actor;
		}
		
		private function enterGameHandler(event:MouseEvent):void
		{
			var packet:Packet = new Packet(true,Group.RegRole,RegRoleManager.EnterGame,PHPSerializer.serialize(currole.id));
			Connect.server.sendPackage(packet);
		}
		
		public function get rolereg():RoleReg
		{
			return this.viewComponent as RoleReg;
		}
		
		override public function listNotificationInterests():Array
		{
			return [CREATEROLE,SHOWROLES];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName()){
				case CREATEROLE:
					rolereg.currentState = "createrole";
					rolereg.createBtn.enabled = true;
					rolereg.nameinput.text = "";
					rolereg.sex.selectedValue = "男";
					this.rolereg.createBtn.addEventListener(MouseEvent.CLICK,createHandler);
					break;
				case SHOWROLES:
					rolereg.currentState = "viewroles";
					rolereg.list.removeAll();
					for each(var actor:Actor in notification.getBody()){
						rolereg.list.addItem(actor);
					}
					this.rolereg.rolelist.addEventListener(MouseEvent.CLICK,selectRoleHandler);
					this.rolereg.enterGameBtn.addEventListener(MouseEvent.CLICK,enterGameHandler);
					this.currole = null;
					this.rolereg.enterGameBtn.enabled = false;
					this.rolereg.rolelist.selectedIndex = -1;
					break;
			}
		}
	}
}